package com.lx.wrapper;

import com.lx.constant.ano.FieldFunction;
import com.lx.constant.consts.CommonConstant;
import com.lx.constant.enums.ConditionKeyWordEnum;
import com.lx.inter.condition.IEveryEntityLambdaDefineCondition;

/**
 * Lambda 抽象执行类 -> 主实体定义条件项,所有条件可在任意实体类下补全
 * <p>
 * 注意,当前
 *
 * @param <MainEntity> 主实体
 * @author lx
 * @create 2022/11/15 18:38
 **/
public abstract class AbstractEveryEntityLambdaConditionWrapper<MainEntity, ChildWrapper extends AbstractEveryEntityLambdaConditionWrapper<MainEntity, ChildWrapper>> extends AbstractCommonConditionWrapper<MainEntity, ChildWrapper> implements IEveryEntityLambdaDefineCondition<ChildWrapper> {

    public AbstractEveryEntityLambdaConditionWrapper(Class<MainEntity> entityClass) {
        super(entityClass);
    }

    @Override
    public <EveryEntity> ChildWrapper eq(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum eq = ConditionKeyWordEnum.EQ;
        checkParamIsValid(eq, val);
        return condition ? addCondition(columnColFn, eq, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper eq(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, AbstractCommonConditionWrapper conditionWrapper) {
        ConditionKeyWordEnum eq = ConditionKeyWordEnum.EQ;
        checkParamIsValid(eq, conditionWrapper);
        return condition ? addCondition(columnColFn, eq, conditionWrapper) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity1, EveryEntity2> ChildWrapper eq(boolean condition, FieldFunction<EveryEntity1, ?> columnColFn, FieldFunction<EveryEntity2, ?> conditionFieldFn) {
        if (!condition) {
            return (ChildWrapper) this;
        }

        ConditionKeyWordEnum eq = ConditionKeyWordEnum.EQ;
        checkParamIsValid(eq, conditionFieldFn);
        return addCondition(columnColFn, eq, conditionFieldFn);
    }

    @Override
    public <EveryEntity> ChildWrapper ne(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum ne = ConditionKeyWordEnum.NE;
        checkParamIsValid(ne, val);

        return condition ? addCondition(columnColFn, ne, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper ne(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, AbstractCommonConditionWrapper conditionWrapper) {
        ConditionKeyWordEnum ne = ConditionKeyWordEnum.NE;
        checkParamIsValid(ne, conditionWrapper);
        return condition ? addCondition(columnColFn, ne, conditionWrapper) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper gt(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum gt = ConditionKeyWordEnum.GT;
        checkParamIsValid(gt, val);

        return condition ? addCondition(columnColFn, gt, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper ge(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum ge = ConditionKeyWordEnum.GE;
        checkParamIsValid(ge, val);

        return condition ? addCondition(columnColFn, ge, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper lt(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum lt = ConditionKeyWordEnum.LT;
        checkParamIsValid(lt, val);

        return condition ? addCondition(columnColFn, lt, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper le(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        ConditionKeyWordEnum le = ConditionKeyWordEnum.LE;
        checkParamIsValid(le, val);

        return condition ? addCondition(columnColFn, le, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper in(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object... valArr) {
        ConditionKeyWordEnum in = ConditionKeyWordEnum.IN;
        checkParamIsValid(in, valArr);

        return condition ? addCondition(columnColFn, in, valArr) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper in(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, AbstractCommonConditionWrapper childWrapper) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.IN, childWrapper) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper notIn(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object... valArr) {
        ConditionKeyWordEnum notIn = ConditionKeyWordEnum.NOT_IN;
        checkParamIsValid(notIn, valArr);

        return condition ? addCondition(columnColFn, notIn, valArr) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper notIn(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, AbstractCommonConditionWrapper childWrapper) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.NOT_IN, childWrapper) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper like(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.LIKE, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper likeLeft(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.LIKE, CommonConstant.PERCENT, val) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper likeRight(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object val) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.LIKE, val, CommonConstant.PERCENT) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper isNull(boolean condition, FieldFunction<EveryEntity, ?> columnColFn) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.IS_NULL) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper isNotNull(boolean condition, FieldFunction<EveryEntity, ?> columnColFn) {
        return condition ? addCondition(columnColFn, ConditionKeyWordEnum.IS_NOT_NULL) : (ChildWrapper) this;
    }

    @Override
    public <EveryEntity> ChildWrapper between(boolean condition, FieldFunction<EveryEntity, ?> columnColFn, Object startVal, Object endVal) {
        ConditionKeyWordEnum between = ConditionKeyWordEnum.BETWEEN;
        checkParamIsValid(between, startVal, endVal);

        return condition ? addCondition(columnColFn, between, startVal, endVal) : (ChildWrapper) this;
    }
}
